import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:imember/Util/Global.dart';
import 'package:imember/Util/MyColor.dart';
import 'package:imember/Util/PageUtil.dart';
import 'package:imember/Widget/MyButton.dart';
import 'package:imember/Widget/MyDialog.dart';
import 'package:imember/Widget/MyImage.dart';
import 'package:imember/Widget/MyInput.dart';

import '../../CameraScan.dart';
import 'SeparatePage.dart';

class InputJC_or_MT extends StatefulWidget {
  int type;

  InputJC_or_MT(this.type);

  @override
  State<StatefulWidget> createState() {
    return _InputJC_or_MT();
  }
}

class _InputJC_or_MT extends State<InputJC_or_MT> {
  int packType = 0;
  static const int Merge = 0;
  static const int Separate = 1;

  List<String> orderList = [""];

  addInput() {
    setState(() {
      orderList.add("");
    });
  }

  @override
  Widget build(BuildContext context) {
    List<TextEditingController> orderInputControllerList = [];
    for (var i = 0; i < orderList.length; i++) {
      orderInputControllerList.add(TextEditingController());
    }
    return Container(
        width: 460,
        child: Column(mainAxisSize: MainAxisSize.min, children: [
          Container(
            height: 52,
            decoration: BoxDecoration(color: MyColor.Orange, borderRadius: BorderRadius.vertical(top: Radius.circular(8))),
            alignment: Alignment.center,
            child: Text(widget.type == 0 ? "機場" : "碼頭", style: TextStyle(fontSize: 24, fontWeight: FontWeight.bold, color: MyColor.White)),
          ),
          Container(
              padding: EdgeInsets.all(16),
              decoration: BoxDecoration(color: MyColor.White, borderRadius: BorderRadius.vertical(bottom: Radius.circular(8))),
              child: Column(children: [
                Row(children: [
                  Button("合併打包",
                      height: 24,
                      width: 100,
                      fontSize: 11,
                      bgColor: packType == Merge ? MyColor.Orange : MyColor.C99,
                      radius: 14, onTap: () {
                    setState(() {
                      packType = Merge;
                    });
                  }),
                  Container(width: 10),
                  Button("分開打包",
                      height: 24,
                      width: 100,
                      fontSize: 11,
                      bgColor: packType == Separate ? MyColor.Orange : MyColor.C99,
                      radius: 14, onTap: () {
                    setState(() {
                      packType = Separate;
                    });
                  })
                ]),
                packType == Merge
                    ? Container(
                        height: 257,
                        margin: EdgeInsets.fromLTRB(12, 18, 12, 24),
                        child: ListView.builder(
                            itemCount: orderList.length + 1,
                            itemBuilder: (BuildContext context, int i) {
                              if (i == orderList.length) {
                                return LocalImage("ic_add_input.png", width: 24, height: 24, padding: EdgeInsets.all(10), onTap: addInput);
                              }
                              return OrderNoInput(i + 1, orderInputControllerList[i], onDelClick: () {
                                setState(() {
                                  orderList.removeAt(i);
                                });
                              }, onCodeClick: () {
                                showWidgetDialog(context, CameraScan((value) {
                                  orderInputControllerList[i].text = value;
                                }));
                              });
                            }))
                    : Container(
                        height: 257,
                        margin: EdgeInsets.fromLTRB(12, 18, 12, 24),
                        padding: EdgeInsets.fromLTRB(16, 16, 16, 0),
                        decoration: Global.getBoxBg(8, MyColor.F5),
                        child: SeparatePage()),
                Row(mainAxisAlignment: MainAxisAlignment.center, children: [
                  Button("取消", height: 28, width: 170, bgColor: MyColor.C99, radius: 14, onTap: () {
                    PageUtil().back(context);
                  }),
                  Container(width: 12),
                  Button("確認", height: 28, width: 170, bgColor: MyColor.Orange, radius: 14),
                ]),
                Container(height: 12),
              ]))
        ]));
  }
}


class OrderNoInput extends StatelessWidget {
  int index;
  TextEditingController controller;
  Function() onDelClick;
  Function() onCodeClick;

  OrderNoInput(this.index, this.controller, {required this.onDelClick, required this.onCodeClick});

  @override
  Widget build(BuildContext context) {
    return Container(
        padding: EdgeInsets.only(top: 12),
        child: Row(
          mainAxisAlignment: MainAxisAlignment.center,
          crossAxisAlignment: CrossAxisAlignment.center,
          children: [
            Text("$index.", style: TextStyle(fontSize: 15, color: MyColor.C99)),
            Container(
              width: 248,
              height: 30,
              margin: EdgeInsets.only(right: 8, left: 8),
              padding: EdgeInsets.fromLTRB(16, 0, 16, 0),
              decoration:
                  BoxDecoration(borderRadius: BorderRadius.all(Radius.circular(24)), border: Border.all(width: 1.2, color: MyColor.C99)),
              child: NormalInput(hintText: "請輸入訂單編號或掃碼", controller: controller),
            ),
            LocalImage("ic_code.png", width: 15, padding: EdgeInsets.all(4), onTap: onCodeClick),
            LocalImage("ic_x_g.png", width: 25, padding: EdgeInsets.all(4), onTap: onDelClick),
          ],
        ));
  }
}
